package com.example.serverfront;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
@CrossOrigin
public class Controller {

    @GetMapping("Celsius")
    public float getCelsius() throws Exception{
        System.out.println("前端请求数据");
        //开始监听
        DatagramSocket datagramSocket = new DatagramSocket(8800);
        System.out.println("该服务绑定在8800端口，用于发送接受UDP包");
        byte [] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length,InetAddress.getByName("127.0.0.1"), 9000);
        datagramSocket.send(packet); //8800端口向9000端口发送数据包，告知要拿一个温度值
        System.out.println("发送完毕，开始接收");
        //接收
        datagramSocket.receive(packet);
        String res = new String(packet.getData(),0, packet.getLength());
        System.out.println(res);
        datagramSocket.close();
        return Float.parseFloat(res);
    }


    private long getCelsiusPort(int port,int myport)throws Exception{
        DatagramSocket datagramSocket1 = new DatagramSocket(myport);
        byte [] buffer = new byte[1024];
        DatagramPacket packet1 = new DatagramPacket(buffer, 0, buffer.length,InetAddress.getByName("127.0.0.1"), port);
        datagramSocket1.send(packet1); //myport端口向port端口发送数据包，告知要拿一个温度值
        datagramSocket1.receive(packet1);
        String res1 = new String(packet1.getData(),0, packet1.getLength());
        datagramSocket1.close();
        return Long.parseLong(res1);
    }

    @GetMapping("/multi/celsius")
    public long [] getMultiCelsius() throws Exception{
        System.out.println("前端请求数据");
        //4个传感器
        int sensorNum  = 4;
        long [] celsius = new long[sensorNum];
        celsius[0] = getCelsiusPort(9000,8880);
        celsius[1] = getCelsiusPort(9001,8881);
        celsius[2] = getCelsiusPort(9002,8882);
        celsius[3] = getCelsiusPort(9003,8883);
        System.out.println("传入的四个温度是：");
        System.out.println(celsius[0]);
        System.out.println(celsius[1]);
        System.out.println(celsius[2]);
        System.out.println(celsius[3]);
        return celsius;
    }

    @GetMapping("webhook")
    public int sendDingDingWebHookMsg() throws  Exception{
        String dingDingToken = "https://oapi.dingtalk.com/robot/send?access_token=d3b93e3ab29dc9e4c1ddda0dd002b2fdc14ef55ceca29c5740bdaf193ea1b993";
        Map<String, Object> json = new HashMap();
        Map<String, Object> text = new HashMap();
        json.put("msgtype", "text");
        long temp = getCelsiusPort(9000,8880);
        if (temp>40){
            text.put("content", "sensor测得温度为：" +temp+"℃！！温度太高啦！警告警告");
        }else{
            text.put("content", "sensor测得温度为：" +temp);
        }

        json.put("text", text);
        System.out.println(JSON.toJSONString(json));
        String status = HttpUtil.sendJSON(dingDingToken, JSON.toJSONString(json));
        JSONObject jo= JSON.parseObject(status);
        if (jo.get("errcode").equals(0)){
            return 0;
        }else return -1;
    }


}
